<template>
    <div class="SHZY-common-layout">
        <div class="SHZY-common-layout-center">
            <el-row class="SHZY-common-search-box" :gutter="16">
                <el-form @submit.native.prevent="">
                    <el-col :span="6">
                        <el-form-item label="物料号">
                            <el-input v-model="query.matnrCode" placeholder="物料号" @keyup.enter.native="search" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="采购类型">
                            <el-select v-model="query.orderType" placeholder="采购类型" @keyup.enter.native="search" clearable>
                                <el-option v-for="(item, index) in orderTypeOptions" :key="index" :label="item.fullName"
                                    :value="item.enCode" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="供应商">
                            <el-input v-model="query.supplierNo" placeholder="供应商" @keyup.enter.native="search" clearable />
                        </el-form-item>
                    </el-col>
                    <template v-if="showAll">
                    <el-col :span="6">
                        <el-form-item label="检验批号">
                            <el-input v-model="query.inspectionlotCode" placeholder="检验批号" @keyup.enter.native="search"
                                clearable />
                        </el-form-item>
                    </el-col>
                        <el-col :span="6">
                            <el-form-item label="跳检规则">
                                <el-input v-model="query.ruleCode" placeholder="跳检规则编号" @keyup.enter.native="search"
                                    clearable />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="阶段">
                                <el-input v-model="query.stage" placeholder="阶段" @keyup.enter.native="search" clearable />
                            </el-form-item>
                        </el-col>
                    </template>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
                            <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
                            <el-button type="text" icon="el-icon-arrow-down" @click="showAll = true"
                                v-if="!showAll">展开</el-button>
                            <el-button type="text" icon="el-icon-arrow-up" @click="showAll = false" v-else>收起</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="SHZY-common-layout-main SHZY-flex-main">
                <div class="SHZY-common-head">
                    <div>
                    </div>
                    <div class="SHZY-common-head-right">
                        <el-tooltip effect="dark" content="刷新" placement="top">
                            <el-link icon="icon-ym icon-ym-Refresh SHZY-common-head-icon" :underline="false"
                                @click="reset()" />
                        </el-tooltip>
                    </div>
                </div>
                <SHZY-table v-loading="listLoading" :data="list" @sort-change='sortChange' custom-column>
                    <el-table-column label="物料号" prop="matnrCode" align="left" min-width="120" :show-overflow-tooltip="true"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="采购类型" prop="orderType" align="left" width="120" sortable='custom'>
                        <template slot-scope="scope">
                            {{ getOrderTypeDes(scope.row.orderType) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="供应商" prop="supplierNo" align="left" min-width="120"
                        :show-overflow-tooltip="true" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="跳检规则" prop="ruleCode" align="left" min-width="120" :show-overflow-tooltip="true"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="阶段" prop="stage" align="left" min-width="120" :show-overflow-tooltip="true"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="合格检数量" prop="qualifiedInspNumber" align="left" min-width="120"
                        sortable='custom'>
                        <template slot-scope="scope">
                            <el-tooltip effect="dark" content="点击查看明细" placement="top">
                                <el-button @click="opendDetail(scope.row.id, 'OK')">{{ scope.row.qualifiedInspNumber
                                }}</el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="不合格数量" prop="unqualifiedInspNumber" align="left" min-width="120"
                        sortable='custom'>
                        <template slot-scope="scope">
                            <el-tooltip effect="dark" content="点击查看明细" placement="top">
                                <el-button @click="opendDetail(scope.row.id, 'NG')">{{ scope.row.unqualifiedInspNumber
                                }}</el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="creatorTime" :formatter="shzy.tableDateFormat" width="120" />
                </SHZY-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
                    @pagination="initData" />
            </div>
        </div>
        <SHZY-Detail v-if="detailVisible" ref="SHZYDetail" />
    </div>
</template>
<script>
import { getExemptionrulerecordList } from '@/api/q1/business/exemptionrulerecord'
import SHZYDetail from './Detail'
export default {
    components: { SHZYDetail },
    name: "business-exemptionrulerecord",
    data() {
        return {
            showAll: false,
            query: {
                matnrCode: undefined,
                orderType: undefined,
                supplierNo: undefined,
                inspectionlotCode:undefined,
                ruleCode: undefined,
                stage: undefined,
            },
            list: [],
            listLoading: true,
            multipleSelection: [],
            total: 0,
            listQuery: {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            },
            detailVisible: false,
            columnList: [
                { prop: 'matnrCode', label: '物料号' },
                { prop: 'orderType', label: '采购类型' },
                { prop: 'supplierNo', label: '供应商' },
                { prop: 'ruleCode', label: '跳检规则编号' },
                { prop: 'stage', label: '阶段' },
                { prop: 'qualifiedInspNumber', label: '合格检验批数量' },
                { prop: 'unqualifiedInspNumber', label: '不合格检验批数量' },
                { prop: 'creatorTime', label: '创建时间' },
            ],
            orderTypeOptions: [],

        }
    },
    computed: {
        menuId() {
            return this.$route.meta.modelId || ''
        }
    },
    created() {
        this.getDictionaryData();
        this.initData()
    },
    methods: {
        getDictionaryData() {
            this.$store.dispatch('base/getDictionaryData', { sort: 'BatchType' }).then((res) => {
                this.orderTypeOptions = res
            })
        },
        getOrderTypeDes(enCode) {
            let orderTypeInfo = this.orderTypeOptions.find(x => x.enCode == enCode)
            if (orderTypeInfo) {
                return orderTypeInfo.fullName
            } else {
                return enCode
            }
        },
        sortChange({ column, prop, order }) {
            this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
            this.listQuery.sidx = !order ? '' : prop
            this.initData()
        },
        initData() {
            this.listLoading = true;
            let _query = {
                ...this.listQuery,
                ...this.query,
                menuId: this.menuId,
            };
            let query = {}
            for (let key in _query) {
                if (Array.isArray(_query[key])) {
                    query[key] = _query[key].join()
                } else {
                    query[key] = _query[key]
                }
            }

            getExemptionrulerecordList(query).then(res => {
                this.list = res.data.list
                this.total = res.data.pagination.total
                this.listLoading = false
            })
        },
        search() {
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        },
        refresh(isrRefresh) {
            if (isrRefresh) this.reset()
        },
        reset() {
            for (let key in this.query) {
                this.query[key] = undefined
            }
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        },
        opendDetail(id, status) {
            this.detailVisible = true
            this.$nextTick(() => {
                this.$refs.SHZYDetail.init(id, status)
            })
        },
    }
}
</script>